const fs =require("fs");
const http =require("http");
const path =require("path");

console.log("hello world",path.resolve(__dirname,"./index.html"));
http.createServer((req,res)=>{
    //1.过滤请求；
    if(req.url ==="/favicon.ico"){
        res.end()
        return false;
    }
    //根据前端发送的路径，拼接路径读取文件信息；

    //拼接合法url路径；
    let url =path.resolve(__dirname,"."+(req.url==="/"?"./index.html":req.url));

    let data =fs.readFileSync(url);

    //3.返回响应
    //3.1写入数据
    res.write(data);
    //3.2终止请求
    res.end()
})
.listen("3000",()=>console.log("服务器已经开启:localhost:3000"));